/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import mapping.ClientMapper;
import network.Network;

/**
 *
 * @author Jasper
 */
public class LoginController extends Thread{
    private DatagramSocket dgSocket;
    private ClientMapper clientMapper;
    
    private  byte[] ipadress = new byte[4];
    private  ServerSocket serverSocket;
    
    //normaal ook nog een method die kijkt of de client reeds beestaat of niet 
    public void makeClient(String name, byte[] ipadress){
        clientMapper.addClient(name, ipadress);
        System.out.println(name + " aangemaakt"); //GUI mag hier ?
        //OverviewController overviewController = new OverviewController(clientMapper);
    }
    
    public void getClient(Socket link){
        try{
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            OutputStream os = link.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);

            String loginNaam;
            loginNaam = input.readLine();

            oos.writeObject(clientMapper.getClient(loginNaam));
            oos.flush();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void updateClients(){
        
        
    }
    
    public List<Client> getRaders()
    {
        return clientMapper.getRaders();
    }
    
    public List<Client> getTekenaars()
    {
        return clientMapper.getTekenaars();
    }
    
    public LoginController(){
        serverSocket = null;
         try{
            //opstarten poort + gaan naar login
             //UDP
            //dgSocket = new DatagramSocket(Network.PORT);
             //TCP
             serverSocket = new ServerSocket(Network.PORT);
            System.out.println("Poort geopend");
            clientMapper = new ClientMapper();
            run();
         }catch(IOException e){
            System.out.println("Kan de poort " + Network.PORT + " niet openen");
            System.out.println("Systeem sluit af...");
            System.exit(1);
        }
         
        System.out.println("Wachten op clients...");
    }
    
    public void waiting(Socket link){
        //while(true){
            try{
                System.out.println("Pakketje ontvangen van host: " + link.getInetAddress().toString());
                
                BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
                PrintWriter output = new PrintWriter(link.getOutputStream(),true);
                
                String loginNaam;
                loginNaam = input.readLine();
                ipadress = link.getInetAddress().getAddress();
                
                System.out.printf("%s wordt aangemaakt\n", loginNaam);
                if(clientMapper.getClient(loginNaam) == null){
                    makeClient(loginNaam, ipadress);
                    output.println("");
                }else{
                    output.println("bestaat reeds");
                }                
                link.setReuseAddress(true);
                serverSocket.setReuseAddress(true);
            }catch(IOException e){
                e.printStackTrace();
            }
    }
    
    public void run() {
        Socket link = null;
        try{
            link = serverSocket.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String opdracht = input.readLine();
            switch(opdracht){
                case "login": waiting(link);break;
                case "getClient": getClient(link);break;
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
    }

}
